/** AppointmentPanel */
package client.ui.panel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import client.EventBus;
import client.events.AddAppointment;
import client.events.ViewPatientInvoice;
import client.events.ViewTR;
import client.service.AppointmentService;
import domain.appointment.Appointment;
import domain.user.SystemUser;

/** Panel containing appointments for a patient or doctor 
 * @author jessienewman
 * @version $Revision: 1.0 $
 */
public class AppointmentPanel extends DefaultPanel {
	/**
	 * Field appointments.
	 */
	private List<Appointment> appointments;
	
	/**
	 * Field c.
	 */
	private final GridBagConstraints c;
	
	/**
	 * Field center.
	 */
	private final JPanel center;
	
	/**
	 * Field addAppointment.
	 */
	private JButton addAppointmentButton;

	/** creates a panel containing appointmetns for a patient or doctor */
	public AppointmentPanel() {
		c = new GridBagConstraints();
		c.ipadx = 30; // $codepro.audit.disable numericLiterals
		center = new JPanel();
		center.setLayout(new GridBagLayout());
		center.setBackground(Color.WHITE);

		c.gridx = 0;
		c.gridy = 0;
		center.add(new JLabel("Patient"), c);
		c.gridx++;
		center.add(new JLabel("Doctor"), c);
		c.gridx++;
		center.add(new JLabel("Date"), c);
		c.gridx++;
		center.add(new JLabel("Time"), c);
		c.gridx++;
		center.add(new JLabel("Treatment Record"), c);
		this.add(center, BorderLayout.CENTER);
	}
	
	/** 
	 * returns the center panel which contains the appointment related parts of the panel 
	
	 * @return jpanel contianing the appointment related parts of the panel */
	public JPanel getCenterPanel() {
		return center;
	}
	
	/**
	 * adds a list of appointments and a user so the page can upadate its information.
	 * sets homePanel to false
	 * @param appointments appointments to be shown
	 * @param u user who's appointments they are
	 */
	public void addAppointments(List<Appointment> appointments, SystemUser u) {
		addAppointments(appointments, u, false);
	}

	/**
	 * determines if the add appointment button should be visible
	 * @param viewable true if the add appointment button should be displayed
	 */
	public void setNewAppointmentViewable(boolean viewable) {
		addAppointmentButton.setVisible(viewable);
	}
	
	/** 
	 * adds a lit of appoitnments which contain the appointment related parts of the panel
	 * @param appointments appintments to display
	 * @param u user whose appointments they are
	 * @param homePanel true if this is going ont he home panel (changes appearance)
	 */
	public void addAppointments(List<Appointment> appointments, 
			final SystemUser u, boolean homePanel) {
		center.removeAll();

		addAppointmentButton = new JButton("New Appointment");
		addAppointmentButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				EventBus.BUS.fireEvent(new AddAppointment(u));
			}
			
		});
		this.appointments = appointments;
		
			c.gridy = 0;
			c.gridx = 2; // $codepro.audit.disable numericLiterals
			c.gridwidth = 2; // $codepro.audit.disable numericLiterals
			c.fill = GridBagConstraints.HORIZONTAL;
			//if (!homePanel) {
				center.add(addAppointmentButton, c);
			//}
			c.gridy++;
		if (appointments.size() != 0) {
			center.add(new JLabel(" "), c);
			c.gridy++;
			c.gridx = 0;
			c.gridwidth = 1;
			center.add(new JLabel("Patient Name"), c);
			c.gridx++;
			center.add(new JLabel("Doctor"), c);
			c.gridx++;
			center.add(new JLabel("Date"), c);
			c.gridx++;
			center.add(new JLabel("Time"), c);
			c.gridx++;
			if (!homePanel) {
				center.add(new JLabel("Treatment Record"), c);
				c.gridx++;
				center.add(new JLabel("Invoice"), c);
			}
			c.gridx = 0;
		} else {
			center.add(new JLabel("(No Previous or Upcoming Appointments)"));
		}
		SimpleDateFormat sdf;
		for (final Appointment a : appointments) {
			c.gridy++;
			c.gridx = 0;
			center.add(new JLabel(a.getPatient().getFullName()), c);
			c.gridx++;
			center.add(new JLabel(a.getDoctor().getFullName()), c);
			c.gridx++;
			center.add(new JLabel(a.getDateString()), c);
			c.gridx++;
			sdf = new SimpleDateFormat("H : mm");
			center.add(new JLabel(sdf.format(a.getDate())), c);
			c.gridx++;
			JButton tr = new JButton("View Record");
			//tr.setBorder(BorderFactory.createEmptyBorder());
			tr.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					a.getTreatmentRecord().setAppointment(a);
					EventBus.BUS.fireEvent(new ViewTR(a.getTreatmentRecord()));
				}
			});
			if (a.getDate().before(new Date()) && !homePanel) {
				center.add(tr, c);
			}
			c.gridx = 5;
			JButton inv = new JButton("View Invoice");
			inv.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					EventBus.BUS.fireEvent(new ViewPatientInvoice(a));
				}
			});
			if (a.getDate().before(new Date()) && !homePanel) {
				center.add(inv, c);
			}
			if (!homePanel && a.getDate().after(new Date())) {
				c.gridx++;
				JButton delete = new JButton("Delete");
				//center.add(delete);
				delete.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent event) {
						AppointmentService.remove(a);
					}
				});
			}
			
		}
	}
}
